package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.spzx.product.domain.Product;
import com.spzx.product.domain.ProductDetails;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.domain.SkuStock;
import com.spzx.product.mapper.ProductDetailsMapper;
import com.spzx.product.mapper.ProductMapper;
import com.spzx.product.mapper.ProductSkuMapper;
import com.spzx.product.mapper.SkuStockMapper;
import com.spzx.product.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-06-15
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Autowired
    private SkuStockMapper skuStockMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Product> selectProductList() {
        return baseMapper.selectProductList();
    }

    @Override
    public int insertProduct(Product product) {

        baseMapper.insert(product);

        List<ProductSku> productSkuList = product.getProductSkuList();

        for (int i = 0, size = productSkuList.size(); i < size; i++) {
            // 商品sku表
            ProductSku productSku = productSkuList.get(i);
            productSku.setSkuCode(product.getId() + "_" + i);
            productSku.setProductId(product.getId());
            productSku.setSkuName(product.getName() + " " + productSku.getSkuSpec());
            productSku.setStatus(0);
            productSkuMapper.insert(productSku);

            //商品sku库存信息表
            SkuStock skuStock = new SkuStock();
            skuStock.setSkuId(productSku.getId());
            skuStock.setTotalNum(productSku.getStockNum());
            skuStock.setLockNum(0);
            skuStock.setAvailableNum(productSku.getStockNum());
            skuStock.setSaleNum(0);
            skuStockMapper.insert(skuStock);
        }

        //商品详情
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(String.join(",", product.getDetailsImageUrlList()));
        productDetailsMapper.insert(productDetails);
        return 1;
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        //商品spu状态
        LambdaUpdateWrapper<Product> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Product::getId, id);
        updateWrapper.set(Product::getStatus, status);
        baseMapper.update(null, updateWrapper);

        /*Product product = new Product();
        product.setId(id);
        product.setStatus(status);
        baseMapper.updateById(product);*/

        //商品sku状态
        LambdaUpdateWrapper<ProductSku> updateWrapperSku = new LambdaUpdateWrapper<>();
        updateWrapperSku
                .eq(ProductSku::getProductId, id)
                .set(ProductSku::getStatus, status);
        productSkuMapper.update(null, updateWrapperSku);

        //根据商品id查询商品skuId列表
        LambdaQueryWrapper<ProductSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductSku::getProductId, id);
        List<Object> productSkuIdList = productSkuMapper.selectObjs(queryWrapper);

        if (status == 1) {
            for (Object skuId : productSkuIdList) {
                redisTemplate.opsForValue().setBit("product:sku:ids",(Long)skuId,true);
            }
        } else if (status == -1) {
            for (Object skuId : productSkuIdList) {
                redisTemplate.opsForValue().setBit("product:sku:ids",(Long)skuId,false);
            }
        }
        return 1;
    }

    @Override
    public int deleteProductByIds(List<Long> ids) {

        //通过id列表删除多个商品信息
        baseMapper.deleteBatchIds(ids);

        //查询skuIds(先查询)
        LambdaQueryWrapper<ProductSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ProductSku::getProductId, ids);

        List<ProductSku> productSkuList = productSkuMapper.selectList(queryWrapper);
        List<Long> skuId = productSkuList.stream().map(ProductSku::getId).toList();

        //删除sku信息
        productSkuMapper.deleteBatchIds(skuId);

        //删除商品详情信息
        LambdaQueryWrapper<ProductDetails> detailsLambdaWrapper = new LambdaQueryWrapper<>();
        detailsLambdaWrapper.in(ProductDetails::getProductId, ids);
        productDetailsMapper.delete(detailsLambdaWrapper);


        //删除库存信息
        LambdaQueryWrapper<SkuStock> SkuStockLambdaWrapper = new LambdaQueryWrapper<>();
        SkuStockLambdaWrapper.in(SkuStock::getSkuId, skuId);
        skuStockMapper.delete(SkuStockLambdaWrapper);

        return 1;

    }
}
